package com.xs.ahoi.hcxs.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * author: ahoi
 * date: 2018/6/21 15:11
 * description:
 */
public class DateUtil {

    public static final String FORMAT_ONLY_HM = "HH:mm";
    public static final String FORMAT_YMD_HM = "YYYY/MM/dd HH:mm";
    public static final String FORMAT_YMD_HMS = "YYYY/MM/dd HH:mm:ss";

    public static Date getDate() {
        return new Date();
    }

    public static long getTime() {
        return new Date().getTime();
    }

    public static String time2String(long time, String pattern) {
        Date date = new Date();
        date.setTime(time);
        return date2String(date, pattern);
    }

    public static String date2String(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    public static Date string2Date(String string, String pattern) {
        Date result = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            result = format.parse(string);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String long2String(long time, String pattern) {
        Date d = new Date();
        d.setTime(time);
        return date2String(d, pattern);
    }

    public static void main(String[] args) {
        long time = 1532583996965l;

        System.out.println(long2String(time, FORMAT_YMD_HM));
    }

}
